package cn.kgc.controller;

import cn.kgc.dto.QueryCondition;
import cn.kgc.entity.*;
import cn.kgc.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/manage")
public class DevUserController {

    @Autowired
    DevUserService devUserService;
    @Autowired
    AppInfoService appInfoService;
    @Autowired
    AppVersionService appVersionService;
    @Autowired
    DataDictionaryService dataDictionaryService;
    @Autowired
    AppCategoryService appCategoryService;

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/";
    }

    @PostMapping("/devuser")
    public String login(String devCode, String devPassword, HttpSession session, RedirectAttributes redirectAttributes) {
        DevUser devUser = devUserService.selectDevUser(devCode, devPassword);
        if (devUser == null) {
            redirectAttributes.addFlashAttribute("error", "用户名或密码错误，请重新输入");
            return "redirect:tologin";
        }else {
            session.setAttribute("devUser",devUser);
            return "redirect:main";
        }
    }

    @GetMapping("/appInfos")
    public String list(QueryCondition queryCondition,
                       HttpSession session,
                       @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                       @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
                       Model model){
        DevUser loginUser = (DevUser) session.getAttribute("devUser");
        PageInfo<AppInfo> appInfos = appInfoService.findAppInfo(queryCondition,loginUser.getId(),pageNum,pageSize);
        List<AppVersion> appVersions = appVersionService.selectAllVersion();
        List<DataDictionary> appStatus = dataDictionaryService.selectByType("APP_STATUS");
        List<DataDictionary> appFlatform = dataDictionaryService.selectByType("APP_FLATFORM");
        List<AppCategory> categoryLevel1 = appCategoryService.findChildCategory(null);
        model.addAttribute("appFlatform",appFlatform);
        model.addAttribute("categoryLevel1",categoryLevel1);
        model.addAttribute("appStatus",appStatus);
        model.addAttribute("appInfos",appInfos);
        model.addAttribute("appVersions",appVersions);
        return "pages/developer/appinfolist";
    }

    @GetMapping("/appInfo")
    public String add(Model model){
//        String typeCode = "APP_FLATFORM";
//        List<DataDictionary> dataList = dataDictionaryService.selectByType(typeCode);
//        model.addAttribute("dataList",dataList);
        return "pages/developer/appinfoadd";
    }

    @PostMapping("/appInfo")
    public String addApp(AppInfo appInfo){
        appInfoService.addApp(appInfo);
        return "pages/developer/appinfolist";
    }
}
